﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;


/// <summary>
/// Summary description for MD5_encrypt
/// </summary>
public  static class MD5_encrypt
{
    //kieu extention su dung theo dang : string md5 = str.ToMD5(); 
    public static string MD5_pass_kieu1(this string str)
    {

        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

        byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

        StringBuilder sbHash = new StringBuilder();

        foreach (byte b in bHash)
        {
            
            sbHash.Append(String.Format("{0:x2}", b));

        }
        return sbHash.ToString();

    }
    //kieu thong thuong: gan gia tri: string md5=MD5_pass_kieu2(giatri);
    /// <summary>
    /// ham ma hoa MD5
    /// </summary>
    /// <param name="pass_word"></param>
    /// <returns></returns>
    public static string MD5_pass_kieu2(string pass_word)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        //thuc hien hash chuoi 
        md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass_word));
        //lay ket qua hash sau khi thuc hien
        byte[] rs = md5.Hash;

        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < rs.Length; i++)
        {
            //bieu dien duoi dang 2 ki so Hexa cho moi byte
            strBuilder.Append(rs[i].ToString("x2"));
        }

        return strBuilder.ToString();

    }
}